package cn.le.crawler.zppt;

import cn.le.annotation.CrawlerName;
import cn.le.crawler.AbstractCrawler;
import cn.le.crawler.Crawler;
import cn.le.entity.CrawlerHome;
import cn.le.enums.EnumHomeStatus;
import cn.le.enums.EnumIsRestricted;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;

import static cn.le.enums.EnumCrawler.Ali;
import static cn.le.enums.EnumCrawler.Zppt;

/**
 * 中拍平台
 */
@Component
@CrawlerName(value = Zppt)
public class ZpptHomeCrawler extends AbstractCrawler implements Crawler {


    @Override
    public List<CrawlerHome> crawlerHome() throws Exception {

        CompletionService<List<ZpptData>> homeCs = new ExecutorCompletionService<List<ZpptData>>(executor);
        List<String> urls = ZpptPropertyUtil.getPageUrls();
       /* urls.forEach(url->{
            JSONObject jsonObject = getProxyJsonResult(url);
            if (jsonObject == null){
                return;
            }
            String itemsStr =  jsonObject.getString("items");
            List<ZpptData> items = JSONArray.parseArray(itemsStr,ZpptData.class);
            //写入获取数据的page url 方便获取地区和拍卖方式
            for (ZpptData data : items) {
                data.setPageUrl(url);
                String reqImgUrl = ZpptPropertyUtil.getReqImgUrl(data.getId(),url);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                jsonObject = getJsonResult(reqImgUrl);
                List<Map<String,String>> picLarge = (List<Map<String, String>>) jsonObject.get("picLarge");
                if (CollectionUtils.isEmpty(picLarge)){
                    continue;
                }
                List<String> imgUrls = new ArrayList<>();
                picLarge.forEach(large->{
                    String imgUrl = large.get("filePath");
                    imgUrl = ZpptPropertyUtil.getImgUrl(imgUrl);
                    imgUrls.add(imgUrl);
                });
                String imgs = JSONArray.toJSONString(imgUrls);
                data.setHomeImgs(imgs);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });*/

        urls.forEach(url->{
            homeCs.submit(new Callable<List<ZpptData>>() {
                @Override
                public List<ZpptData> call() throws Exception {
                    JSONObject jsonObject = getProxyJsonResult(url);
                    String itemsStr =  jsonObject.getString("items");
                    List<ZpptData> items = JSONArray.parseArray(itemsStr,ZpptData.class);
                    //写入获取数据的page url 方便获取地区和拍卖方式
                    for (ZpptData data : items) {
                        data.setPageUrl(url);
                    }
                    return items;
                }
            });
        });

        CompletionService<ZpptData> homeImgCs = new ExecutorCompletionService<ZpptData>(executor);
        int index = 0;
        for (int i = 0,size = urls.size(); i < size; i++) {
            List<ZpptData> list = homeCs.take().get();
            if (CollectionUtils.isEmpty(list)){
                continue;
            }
            list.forEach(data->{
                homeImgCs.submit(new Callable<ZpptData>() {
                    @Override
                    public ZpptData call() throws Exception {
                        String url = ZpptPropertyUtil.getReqImgUrl(data.getId(),data.getPageUrl());
                        JSONObject jsonObject = getProxyJsonResult(url);
                        List<Map<String,String>> picLarge = (List<Map<String, String>>) jsonObject.get("picLarge");
                        if (CollectionUtils.isEmpty(picLarge)){
                            return data;
                        }
                        List<String> imgUrls = new ArrayList<>();
                        picLarge.forEach(large->{
                            String imgUrl = large.get("filePath");
                            imgUrl = ZpptPropertyUtil.getImgUrl(imgUrl);
                            imgUrls.add(imgUrl);
                        });
                        String imgs = JSONArray.toJSONString(imgUrls);
                        data.setHomeImgs(imgs);
                        return data;
                    }
                });
            });
            index += list.size();
        }
        List<CrawlerHome> crawlerHomes = new LinkedList<>();
        for (int i = 0; i < index; i++) {
            ZpptData data = homeImgCs.take().get();
            CrawlerHome crawlerHome = mapperCrawlerHome(data);
            crawlerHomes.add(crawlerHome);
        }
        return new ArrayList<>();
    }

    public CrawlerHome mapperCrawlerHome(ZpptData data){
        CrawlerHome crawlerHome = new CrawlerHome();
        BeanUtils.copyProperties(data,crawlerHome);

        crawlerHome.setHomeId(data.getId());
        crawlerHome.setTitle(data.getName());
        crawlerHome.setStatus(EnumHomeStatus.ZpptStatus.getByCode(data.getStatus() == null ? data.getLotStatus() : data.getStatus()).getText());
        crawlerHome.setArea(ZpptPropertyUtil.getArea(data.getPageUrl()));
        crawlerHome.setCover(data.getPic());
        crawlerHome.setInitialPrice(data.getStartPrice());
        crawlerHome.setCurrentPrice(data.getNowPrice());
        crawlerHome.setConsultPrice(data.getMarketPrice());
        crawlerHome.setSellOff("拍卖");
        crawlerHome.setIsRestricted(EnumIsRestricted.ZpptRestricted.getByCode(data.getIsRestricted()).getText());
        crawlerHome.setViewerCount(data.getOnLooker());
        crawlerHome.setBidCount(data.getEnrollment());
        crawlerHome.setSource("中拍平台");
        crawlerHome.setCreateDate(new Date());
        return crawlerHome;
    }
}
